<?php 
	namespace Admin\Controller;
	use Think\Controller;

	class AdverController extends CommonController{
		public function index()
		{
			$model=M('advertisement');
			//总条数
			$total=$model->count();
			//echo $total;exit;
			//总页数
			$page=new \Think\Page($total,2);
			$page->setConfig('header','总页数');
			$page->setConfig('prev','上一页');
			$page->setConfig('next','下一页');
			//获取当前页
			$p=$page->nowPage;
			//echo $p;
			//exit;
			$page_str=$page->show();
			$limit=$page->firstRow.','.$page->listRows;

			$list=$model->limit($limit)->select();
			foreach ($list as $key => $value) 
			{
				$list[$key]['name']='/Public/Uploads/'.$this->sub($value['name']).'80_'.$value['name'];
			}
			//var_dump($list);exit;
			//echo $page_str;exit;
			$this->assign('page',$page_str);
			$this->assign('p',$p);
			$this->assign('list',$list);
			$this->display();
		}

	//评论在前台是否显示
		public function check()
		{
			//接受当前页
			$p=$_GET['p'];

			$where['id']=$_GET['id'];
			$model=M('advertisement');
			//先查询 同一个广告位在前台只显示一张图
			$location=$model->where($where)->getField('location');
			$wh['location']=$location;
			$data['status']=0;
			$res=$model->where($wh)->save($data);

			$dat['status']=1;
			$res1=$model->where($where)->save($dat);
			//var_dump($res1);exit;
			if ($res1) 
			{
				$this->redirect('Adver/index',['p'=>$p]);
				return false;
			}

		}

		public function del(){
			$where['id']=$_GET['id'];
			//查询图片
			$model=M('advertisement');
			$list=$model->where($where)->getField('name');
			//var_dump($list);exit;
			$res=$model->where($where)->delete();

			if ($res) 
			{	//删除路径下的图片				
				$this->setUnlink($list);
				$this->success('删除成功！',U('Adver/index'),1);
			}
		}

		//添加广告页面
		public function add()
		{
			$this->display();
		}
		//添加广告
		public function addimg()
		{
			$data['name']=$this->upload();
			$data['location']=$_POST['location'];
			$model=M('advertisement');
			//插入数据库
			$res=$model->add($data);
			if ($res) 
			{
				$this->success('添加成功！',U('Adver/index'),1);
				return false;
			}else{
				$thisi->error('广告添加失败！');
				return false;
			}
		}

		//文件上传
		public function upload(){
			//实例化上传类
			$upload=new \Think\Upload();
			//设置附件上传大小
			$upload->maxSize=3145728;
			//设置附件上传类型
			$upload->exts=array('jpg','gif','png','jpeg');
			//设置附件上传目录
			$upload->rootPath='./Public/';
			$upload->savePath='./Uploads/';
			$upload->saveName=date('Ymd',time()).uniqid();
			$upload->autoSub=true;
			$upload->subName=date('Y',time()).'/'.date('m',time()).'/'.date('d',time());
			//上传文件,返回成功上传的文件信息数组
			$info=$upload->upload();
			/*var_dump($info);
			exit;
			*/
			if (!$info) 
			{
				//上传错误提示错误信息
				$this->error($upload->getError());
			}else{
				//上传成功
				//做图片压缩
				$this->zoom($info['pic']['savename']);
				return $info['pic']['savename'];
			}
		}

		private function sub($str)
		{
			return substr($str, 0,4).'/'.substr($str, 4,2).'/'.substr($str, 6,2).'/';
		}

		private function zoom($filename)
		{
			$image=new \Think\Image();
			$path='./Public/Uploads/'.$this->sub($filename);
			$image->open($path.$filename);
			//按照原图的比例生成一个缩略图并保存
			$image->thumb(233,322)->save($path.'322_'.$filename);
			$image->thumb(150,150)->save($path.'150_'.$filename);
			$image->thumb(300,300)->save($path.'300_'.$filename);
			$image->thumb(98,125)->save($path.'125_'.$filename);
			$image->thumb(80,80)->save($path.'80_'.$filename);
		}

		private function setUnlink($filename){
			$path='./Public/Uploads/';
			unlink($path.$this->sub($filename).$filename);
			unlink($path.$this->sub($filename).'150_'.$filename);
			unlink($path.$this->sub($filename).'322_'.$filename);
			unlink($path.$this->sub($filename).'300_'.$filename);
			unlink($path.$this->sub($filename).'125_'.$filename);
			unlink($path.$this->sub($filename).'80_'.$filename);
		}

	}

?>